package it.unisa.services;

import it.unisa.beans.Account;
import it.unisa.beans.Comment;
import it.unisa.beans.Petroglyph;
import it.unisa.beans.Photo;
import it.unisa.dao.AccountDao;
import it.unisa.dao.CommentDao;
import it.unisa.dao.PetroglyphDao;
import it.unisa.dao.PhotoDao;

import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * TODO metodo rimozione commento (id) by owner or superuser
 * @author Alfonso
 *
 */
public class CommentWS  {

	public void addComment(Comment comment) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		CommentDao commentDao=(CommentDao)appContext.getBean("commentDao");
		comment.setDate(new Date(0));
		comment.setHour(new Time(0));
		commentDao.insert(comment);
	}
	
	public it.unisa.beanService.Comment[] viewCommentByPhoto(int idPhoto) {
		
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		PhotoDao photo=(PhotoDao)appContext.getBean("photoDao");
		Photo p = new Photo();
		p.setId(idPhoto);
		Set<Comment> lista = photo.find(p).get(0).getComments();
		it.unisa.beanService.Comment[] arr = new it.unisa.beanService.Comment[lista.size()];
		int i=0;
		for (Comment c : lista) {
			it.unisa.beanService.Comment ex = new it.unisa.beanService.Comment();
			ex.setId(c.getId());
			ex.setDate(c.getDate().toString());
			ex.setHour(c.getHour().toString());
			ex.setText(c.getText());
			ex.setValuation(c.getValuation());
			ex.setAccount(c.getAccount().getSsn());
			ex.setPhoto(c.getPhoto().getId());	
			arr[i] = ex;
			i++;
		}
		return arr;
	}
	
	public it.unisa.beanService.Comment[] viewCommentbyAccount(String account) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		AccountDao acc = (AccountDao)appContext.getBean("accountDao");
		Account acco = new Account();
		acco.setNickname(account);
		Set<Comment> lista = acc.find(acco).get(0).getComments();
		it.unisa.beanService.Comment[] arr = new it.unisa.beanService.Comment[lista.size()];
		int i=0;
		for (Comment c : lista) {
			it.unisa.beanService.Comment ex = new it.unisa.beanService.Comment();
			ex.setId(c.getId());
			ex.setDate(c.getDate().toString());
			ex.setHour(c.getHour().toString());
			ex.setText(c.getText());
			ex.setValuation(c.getValuation());
			ex.setAccount(c.getAccount().getSsn());
			ex.setPhoto(c.getPhoto().getId());	
			arr[i] = ex;
			i++;
		}
		return arr;
	}
	
	public it.unisa.beanService.Comment[] viewCommentByPetroglyph(int idPetro) {
		List<Set<Comment>> lista=new ArrayList<Set<Comment>>();
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		PetroglyphDao petroglyphDao=(PetroglyphDao)appContext.getBean("petroglyphDao");
		Petroglyph p = new Petroglyph();
		p.setId(idPetro);
		Petroglyph petroglyph=petroglyphDao.find(p).get(0);
		int n = 0;
		for(Photo photo:petroglyph.getPhotos()){
			lista.add(photo.getComments());
			n+=photo.getComments().size();
		}
		
		it.unisa.beanService.Comment[] arr = new it.unisa.beanService.Comment[n];
		for(int j = 0;j<lista.size(); j++) {
			int i=0;
			for (Comment c : lista.get(j)) {
				it.unisa.beanService.Comment ex = new it.unisa.beanService.Comment();
				ex.setId(c.getId());
				ex.setDate(c.getDate().toString());
				ex.setHour(c.getHour().toString());
				ex.setText(c.getText());
				ex.setValuation(c.getValuation());
				ex.setAccount(c.getAccount().getSsn());
				ex.setPhoto(c.getPhoto().getId());	
				arr[i] = ex;
				i++;
			}
		}
		return arr;
	}

	public boolean removeComment(Comment comment){
		return true;
	}

}
